<?php

namespace Gxk3\Controller;

class UserjsonController extends CommonController {
	public function __construct() {
		parent::__construct ();
		parent::__config ( array (
				'model_name' => 'User' 
		) );
	}
	
	public  function login(){
			
// 		$userModel = D("User");
			$adminModel = D ( "User" );
			$where_find ['username'] = I ( 'username' );
			$user_find = $adminModel->where ( $where_find )->field('id,password,username,end_time,is_fee')->find ();
			if (! $user_find) {
				$json_result['status_code']=101;//找不到用户
				$json_result['message']="该用户不存在";//找不到用户
				$json_result['result']='';//找不到用户 
				echo json_encode($json_result,JSON_UNESCAPED_UNICODE);
			}else {
				if($user_find['status']==1){
					$json_result['status_code']=103;//找不到用户
					$json_result['message']="被禁止登录";//找不到用户
					$json_result['result']='';
					echo json_encode($json_result,JSON_UNESCAPED_UNICODE);
					exit();
				}
// 				//已经登录过
// 				if(S($user_find['id'])){
// 					if(S($user_find['id']."_real_id")==get_real_ip()){//同Ip,同用户不给重新登录
// 						$json_result['status_code']=105;//已经登录过
// 						$json_result['message']="本机已经登录过";
// 						$json_result['result']='';
// 						echo json_encode($json_result,JSON_UNESCAPED_UNICODE);
// 						exit();
// 					}
// 				}
				
				if (decrypt ( $user_find ['password'] ) == I ( 'password' )) {
					$session_info['username']=$user_find['username'];
					$session_info['id']=$user_find['id'];
					$session_info['user_rank']=$user_find['user_rank'];
					$session_info['grant']=$user_find['grant'];
					$str = $this->randCode(6,2);
					$session_id = md5($user_find['id']).$str;
					S($session_id,$session_info,3600*24*1000);//缓存登录状态
					S($user_find['id'],$session_id,3600*24*1000);//修改用户有效果id
					S($user_find['id']."_real_id",get_real_ip(),3600*24*1000);//缓存下用户真实ip
					
					$json_result['status_code']=100;//成功登录
					$json_result['message']="成功登录";
					$json_result['result']["session_id"]=$session_id;
					unset($user_find['password']);//去掉密码
					$json_result['result']["user_info"]=$user_find;
					echo json_encode($json_result,JSON_UNESCAPED_UNICODE);
				}else{
					$json_result['status_code']=101;//找不到用户
					$json_result['message']="密码错误";//找不到用户
					$json_result['result']='';
					echo json_encode($json_result,JSON_UNESCAPED_UNICODE);
				}
			}
		
	}
	
	public function logout(){
		$session_id = I("session_id");
// 		dump(S($session_id));
// 		die();
// 		dump($session_id);
		$user_info = S($session_id);
// 		dump($user_info);
		S($user_info['id'],null);
		S($session_id,null);
// 		dump(S($session_id));
// 		dump(S($user_info['id']));
	}
	
	public function fdata(){
// 		$session_id = I("session_id");
// 		$username = I("session_id");
// 		$user_info = S($session_id);
// 		$str_time = dec62(msectime());
// 		dump($str_time);
		echo $this->randCode(6,2);
	}
	
	function randCode($length = 5, $type = 0) {
		$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
		if ($type == 0) {
			array_pop($arr);
			$string = implode("", $arr);
		} elseif ($type == "-1") {
			$string = implode("", $arr);
		} else {
			$string = $arr[$type];
		}
		$count = strlen($string) - 1;
		$code = '';
		for ($i = 0; $i < $length; $i++) {
			$code .= $string[rand(0, $count)];
		}
		return $code;
	}
	
	public function get_time(){
		$result['current_time']=time();
		echo json_encode($result);
		exit();
	}
	
	public function index() {
		$this->display ();
	}
	public function ad() {
		$this->display ();
	}
	public function newsadright() {
		$this->display ();
	}
	public function register() {
		$this->display ();
	}
	public function changpwd() {
		$this->display ();
	}
	public function getpwd() {
		$this->display ();
	}
	public function changpaypwd() {
		$this->display ();
	}
	public function getpaypwd() {
		$this->display ();
	}
	public function contact() {
		$this->display ();
	}
	
}